package com.seex.motor;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestMotorActivity extends Activity implements Handler.Callback {
    static final String TAG = TestMotorActivity.class.getSimpleName();

    private TextView tv_motor_status;
    private TextView tv_run_status;
    private Button btn_open;
    private Button btn_stop;
    private Button btn_close;
    private Button btn_forward;
    private Button btn_cycle;
    private Button btn_reverse;
    private Motor mMotor;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new Handler(this);
        mMotor = new Motor();
        mMotor.setHandler(mHandler);
        setContentView(R.layout.test_motor_activity_main);
        initView();
    }


    private void initView() {
        tv_motor_status = (TextView) findViewById(R.id.tv_motor_status);
        tv_run_status = (TextView) findViewById(R.id.tv_run_status);
        btn_open = (Button) findViewById(R.id.btn_open);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        btn_close = (Button) findViewById(R.id.btn_close);
        btn_forward = (Button) findViewById(R.id.btn_forward);
        btn_cycle = (Button) findViewById(R.id.btn_cycle);
        btn_reverse = (Button) findViewById(R.id.btn_reverse);
        setEnable(false, btn_cycle, btn_reverse, btn_forward, btn_close, btn_stop);
    }

    @Override
    public boolean handleMessage(Message msg) {

        switch (msg.what) {
            case Motor.MSG_RUN_STOP:
                tv_motor_status.setText("电机暂停");
                btn_stop.setEnabled(false);
                break;
            case Motor.MSG_RUN_START:
                tv_motor_status.setText("电机开始");
                btn_stop.setEnabled(false);
                break;
            case Motor.MSG_POINT_START:
                tv_run_status.setText("到达启点");
                break;
            case Motor.MSG_POINT_END:
                tv_run_status.setText("到达终点:");
                break;

        }
        return false;
    }

    public void open(View view) {
        boolean open = mMotor.open();
        if (open) {
            tv_motor_status.setText("电机已开启");
            btn_open.setEnabled(false);
            setEnable(true, btn_cycle, btn_reverse, btn_forward, btn_close, btn_stop);

        } else {
            tv_motor_status.setText("电机开启失败");
            btn_open.setEnabled(true);
            setEnable(false, btn_cycle, btn_reverse, btn_forward, btn_close, btn_stop);
        }

    }

    public void netStart(View view) {
        mMotor.start();
        mMotor.netStart();
    }

    public void netStop(View view) {
        mMotor.stop();
        mMotor.netStop();
    }

    boolean enable;

    public void enableKey(View view) {
        enable = !enable;
        mMotor.enableKey(enable);
    }

    public void close(View view) {
        mMotor.close();
        tv_motor_status.setText("电机已关闭");
        btn_open.setEnabled(true);
    }

    public void forwardRun(View view) {
        mMotor.forwardRun();
        btn_stop.setEnabled(false);
        setEnable(false, btn_cycle, btn_reverse);
        setEnable(true, btn_stop);

    }

    public void reverseRun(View view) {
        mMotor.reverseRun();
        setEnable(false, btn_cycle, btn_forward);
        setEnable(true, btn_stop);

    }


    public void startCycleRun(View view) {
        mMotor.startCycleRun();
        setEnable(false, btn_reverse, btn_forward);
        setEnable(true, btn_stop);
    }


    public void stop(View view) {
        mMotor.stop();
        setEnable(true, btn_reverse, btn_forward, btn_cycle);

    }

    public void startBackTest(View view) {
        stop(view);
        startService(new Intent(this, TestService.class));
        finish();
    }

    public void stopBackTest(View view) {
        stopService(new Intent(this, TestService.class));
    }


    private void setEnable(boolean flag, View... views) {
        for (View view : views) {
            view.setEnabled(flag);
        }
    }

}
